package com.example.learnspringai.controller;

import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;


@Slf4j
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class ChatController {

    private final ChatClient client;

    @GetMapping(value = "/chatCall/{prompt}")
    public String chatCall(@PathVariable String prompt) {
        return client.prompt().user(prompt).call().content();
    }

    /**
     * 流式输出
     * 区分不同会话要传入会话id,每个会话对一个会话id,根据会话id能保持会话记忆
     */
    @GetMapping(value = "/chatFlux/{prompt}/{chatId}", produces = "text/html;charset=utf-8")
    public Flux<String> chatFlux(@PathVariable String prompt, @PathVariable String chatId) {
        return client.prompt()
                .user(prompt)
                .advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
    }
}
